
using UnityEngine;
using System.Collections;

public class upgradeGUI : MonoBehaviour
{
    public Texture2D lasericon;
    public Texture2D hovericon;
    public Texture2D multilasericon;
    public Texture2D shotgunlasericon;
    public Texture2D rocketicon;
    public Texture2D orbicon;
    public Texture2D voidicon;
    public Texture2D icon;
    public Texture2D iconResBuild;
    public Texture2D iconsattBuild2;
    public Texture2D icondefBuild3;
    public Texture2D[] myship;
	
	public Texture2D plus;
	
    private string userinput;
    //statbars mStatbars;
    private int fragments;
    public GUISkin MenuSkin;
    public GUIStyle GUI1;
    bool mainattack1;
    bool mainattack2;
    bool mainattack3;
    bool altattack1;
    bool altattack2;
    bool altattack3;
    public GUIStyle myGUIStyle;
    //public Texture2D textattack;
    private int UpgradeCost = 5;
    private int UpgradeCost2 = 25;
    private int labelLength = 100;
    private int upgradeInterval = 2;
    private Rect windowRect = new Rect(0, 0, 120, 50);
    int shipCounter;

    //statbars stuff
    private int healthBarLength;
    private int damageBarLength;
    private int armorBarLength;
    private int supportBarLength;
    private int evasionBarLength;
    private int fireRateBarLength;
    public static int textureBarLength = 100;

    private int citybarres;
    private int citybardef;
    private int citybarass;

    public Texture2D background; // Allows you to place a textuer in, in the Inspector
    public Texture2D foreground;//Allows you to place a texture in, in th;e inspector
    public Texture2D resbuild;
    public Texture2D attbuild;
    public Texture2D defbuild;
    public Texture2D textattack;
    public Texture2D textattack2;
    public static bool upgradeEnabled = false;
    public static bool onStart = true;

    void customStart()
    {
        windowRect = new Rect(0, 0, Screen.width, Screen.height);
        fragments = PlayerPrefs.GetInt("Currency");
        shipCounter = 0;

        //Statbars stuff
        healthBarLength = PlayerPrefs.GetInt("HealthBoost");
        damageBarLength = PlayerPrefs.GetInt("DamageBoost");
        fireRateBarLength = PlayerPrefs.GetInt("FireRate");
        armorBarLength = PlayerPrefs.GetInt("Armor") * 10;
        //if(PlayerPrefs.GetInt("Evasion") > 0)
        evasionBarLength = PlayerPrefs.GetInt("Evasion") * 2;
        citybardef = PlayerPrefs.GetInt("ShieldBoost");
        citybarres = PlayerPrefs.GetInt("ResidentialBoost");
        citybarass = PlayerPrefs.GetInt("AttackBoost") * 10;

        switch (Controller.mainAttack)
        {
            case Controller.mainAttacks.Laser:
                mainattack1 = true;
                mainattack2 = false;
                mainattack3 = false;
                break;
            case Controller.mainAttacks.Multilaser:
                mainattack1 = false;
                mainattack2 = true;
                mainattack3 = false;
                break;
            case Controller.mainAttacks.Shottylaser:
                mainattack1 = false;
                mainattack2 = false;
                mainattack3 = true;
                break;
            default:
                mainattack1 = true;
                break;
        }
        switch (Controller.secondaryAttack)
        {
            case Controller.secondaryAttacks.Rocket:
                altattack1 = true;
                altattack2 = false;
                altattack3 = false;
                break;
            case Controller.secondaryAttacks.Clusterbomb:
                altattack1 = false;
                altattack2 = true;
                altattack3 = false;
                break;
            case Controller.secondaryAttacks.Vortex:
                altattack1 = false;
                altattack2 = false;
                altattack3 = true;
                break;
            default:
                altattack1 = true;
                break;
        }


    }
    void shipStats()
    {
         //B2 Bomber is stronger and more evasive
        if (PlayerPrefs.GetString("PlayerShip") == "B2Bomber")
        {
            PlayerPrefs.SetInt("DamageBoost", 10);
            PlayerPrefs.SetInt("Evasion", 20);
        }
        // Gold ship gives you more currency and higher drops

        //Random bonuses up to 10
        if (PlayerPrefs.GetString("PlayerShip") == "Regs")
        {
            PlayerPrefs.SetInt("HealthBoost", Random.Range(0, 11));
            PlayerPrefs.SetInt("DamageBoost", Random.Range(0, 11));
            PlayerPrefs.SetInt("FireRate", Random.Range(0, 11));
            PlayerPrefs.SetInt("Armor", Random.Range(0, 2));
            PlayerPrefs.SetInt("Evasion", Random.Range(0, 6));
        }

        //City Bonuses
        if (PlayerPrefs.GetString("PlayerShip") == "ThisIsSparta")
        {
            PlayerPrefs.SetInt("ShieldBoost", 10);
            PlayerPrefs.SetInt("ResidentialBoost", 10);
            PlayerPrefs.SetInt("AttackBoost", 1);
        }

    }

    void OnGUI()
    {
        if (onStart)
        {
            shipStats();
            customStart();
            onStart = false;
        }
        if(upgradeEnabled)
        {
            windowRect = GUI.Window(0, windowRect, DoWork, "");
        }
    }
    IEnumerator PauseResume(float wait)
    {
        Time.timeScale = 0.00000001f;
        float pauseEndTime = Time.realtimeSinceStartup + .01f;
        while (Time.realtimeSinceStartup < pauseEndTime)
        {
            yield return 0;
        }
        Time.timeScale = 1;
        Controller.shootSecondary = true;
    }
    public void DoWork(int windowID)
    {
        //mStatbars = gameObject.GetComponent<statbars>();
        
        //GUI.skin = MenuSkin;
        GUI.Box(new Rect(Screen.width / 2 - 375, Screen.height / 2 - 280, 560, 300), "Ship Stats", myGUIStyle);
        GUI.Label(new Rect(Screen.width / 2 - 360, Screen.height / 2 - 100, labelLength, 30), "health");
		GUI.Label(new Rect(Screen.width / 2 - 300, Screen.height / 2 - 80, labelLength, 30), healthBarLength.ToString());
		GUI.Button(new Rect(Screen.width / 2 - 200, Screen.height / 2 - 100, 30, 30), UpgradeCost.ToString());
        if (GUI.Button(new Rect(Screen.width / 2 - 170, Screen.height / 2 - 100, 30, 30), plus))
        {
            if (healthBarLength <= 80 && (fragments - UpgradeCost) >= 0)
            {
                fragments -= UpgradeCost;
                healthBarLength += upgradeInterval;
            }
            if (healthBarLength >= 80 && healthBarLength < textureBarLength && (fragments - UpgradeCost2) >= 0)
            {
                fragments -= UpgradeCost2;
                healthBarLength += upgradeInterval;
            }
        }
        GUI.Label(new Rect(Screen.width / 2 - 360, Screen.height / 2 - 60, labelLength, 30), "damage");
				GUI.Label(new Rect(Screen.width / 2 - 300, Screen.height / 2 - 40, labelLength, 30), damageBarLength.ToString());

			GUI.Button(new Rect(Screen.width / 2 - 200, Screen.height / 2 - 60, 30, 30),UpgradeCost.ToString());
        if (GUI.Button(new Rect(Screen.width / 2 - 170, Screen.height / 2 - 60, 30, 30), plus))
        {

            if (damageBarLength <= 80 && (fragments - UpgradeCost) >= 0)
            {
                fragments -= UpgradeCost;
                damageBarLength += upgradeInterval;
            }
            if (damageBarLength >= 80 && damageBarLength < textureBarLength && (fragments - UpgradeCost2) >= 0)
            {
                fragments -= UpgradeCost2;
                damageBarLength += upgradeInterval;
            }
        }
        GUI.Label(new Rect(Screen.width / 2 - 360, Screen.height / 2 - 20, labelLength, 30), "armor");
						GUI.Label(new Rect(Screen.width / 2 - 300, Screen.height / 2, labelLength, 30), armorBarLength.ToString());

					GUI.Button(new Rect(Screen.width / 2 -200, Screen.height / 2 - 20, 30, 30), UpgradeCost.ToString());
        if (GUI.Button(new Rect(Screen.width / 2 - 170, Screen.height / 2 - 20, 30, 30), plus))
        {
            if (armorBarLength <= 80 && (fragments - UpgradeCost) >= 0)
            {
                fragments -= UpgradeCost;
                armorBarLength += upgradeInterval;
            }
            if (armorBarLength >= 80 && armorBarLength < textureBarLength && (fragments - UpgradeCost2) >= 0)
            {
                fragments -= UpgradeCost2;
                armorBarLength += upgradeInterval;
            }
        }
        GUI.Label(new Rect(Screen.width / 2 - 140, Screen.height / 2 - 60, labelLength, 30), "evasion");
						GUI.Label(new Rect(Screen.width / 2 - 80, Screen.height / 2 - 40, labelLength, 30), evasionBarLength.ToString());

					GUI.Button(new Rect(Screen.width / 2 +20, Screen.height / 2 - 60, 30, 30), UpgradeCost.ToString());
        if (GUI.Button(new Rect(Screen.width / 2 + 50, Screen.height / 2 - 60, 30, 30), plus))
        {

            if (evasionBarLength <= 80 && (fragments - UpgradeCost) >= 0)
            {
                fragments -= UpgradeCost;
                evasionBarLength += 20;
            }
            if (evasionBarLength >= 80 && evasionBarLength < textureBarLength && (fragments - UpgradeCost2) >= 0)
            {
                fragments -= UpgradeCost2;
                evasionBarLength += 20;
            }
        }
        GUI.Label(new Rect(Screen.width / 2 - 140, Screen.height / 2 - 100, labelLength, 30), "support");
						GUI.Label(new Rect(Screen.width / 2 - 80, Screen.height / 2 -80, labelLength, 30), supportBarLength.ToString());

					GUI.Button(new Rect(Screen.width / 2 + 20, Screen.height / 2 - 100, 30, 30), UpgradeCost.ToString());
        if (GUI.Button(new Rect(Screen.width / 2 + 50, Screen.height / 2 - 100, 30, 30), plus))
        {


            if (supportBarLength <= 80 && (fragments - UpgradeCost) >= 0)
            {
                fragments -= UpgradeCost;
                supportBarLength += upgradeInterval;
            }
            if (supportBarLength >= 80 && supportBarLength < textureBarLength && (fragments - UpgradeCost2) >= 0)
            {
                fragments -= UpgradeCost2;
                supportBarLength += upgradeInterval;
            }
        }
        GUI.Label(new Rect(Screen.width / 2 - 140, Screen.height / 2 - 20, labelLength, 30), "fire rate");
						GUI.Label(new Rect(Screen.width / 2 - 80, Screen.height / 2 , labelLength, 30), fireRateBarLength.ToString());

					GUI.Button(new Rect(Screen.width / 2 + 20, Screen.height / 2 -20, 30, 30), UpgradeCost.ToString());
        if (GUI.Button(new Rect(Screen.width / 2 + 50, Screen.height / 2 - 20, 30, 30), plus))
        {
            upgradeInterval = 5;
            if (fireRateBarLength <= 80 && (fragments - UpgradeCost) >= 0)
            {
                fragments -= UpgradeCost;
                fireRateBarLength += upgradeInterval;
            }
            if (fireRateBarLength >= 80 && fireRateBarLength < textureBarLength && (fragments - UpgradeCost2) >= 0)
            {
                fragments -= UpgradeCost2;
                fireRateBarLength += upgradeInterval;
            }
            upgradeInterval = 2;
        }
        GUI.Label(new Rect(Screen.width / 2 - 100, Screen.height / 2 - 230, labelLength, 30), "Ship Name");
        GUI.Label(new Rect(Screen.width / 2 - 100, Screen.height / 2 - 160, labelLength, 30), "Alien Fragments");
        GUI.Label(new Rect(Screen.width / 2 - 320, Screen.height / 2 - 260, 140, 140), icon);//shipbox
		
		////
		if(PlayerPrefs.GetString("PlayerShip") == "Regs")
		GUI.Label(new Rect(Screen.width / 2 - 320, Screen.height / 2 - 260, 140, 140), myship[0]);
		
		if(PlayerPrefs.GetString("PlayerShip") == "B2Bomber")
        GUI.Label(new Rect(Screen.width / 2 - 320, Screen.height / 2 - 260, 140, 140),myship[1] );
	
		if(PlayerPrefs.GetString("PlayerShip") == "Goldy")
        GUI.Label(new Rect(Screen.width / 2 - 320, Screen.height / 2 - 260, 140, 140), myship[2]);
		
		if(PlayerPrefs.GetString("PlayerShip") == "ThisIsSparta")
        GUI.Label(new Rect(Screen.width / 2 - 320, Screen.height / 2 - 260, 140, 140),myship[3] );

		
		
		///
        if (GUI.Button(new Rect(Screen.width / 2 - 180, Screen.height / 2 - 260, 30, 30), plus))
        {
            shipCounter++;
            if (shipCounter >= 4)
            {
                shipCounter = 0;
            }
        }

        // We provide the name of the Style we want to use as the last argument of the Control function
        //GUILayout.BeginArea( (new Rect (0,0,200,60), MenuSkin.GetStyle(MyCustomControl));

        //GUILayout.Button ("I am a regular Automatic Layout Button");
        //GUILayout.Button ("My width has been overridden", GUILayout.Width (95));
        //	GUILayout.EndArea();
        // We can also ignore the Custom Style, and use the Skin's default Button Style
        //GUILayout.Button("MyCustomControl",new Rect(100,100,100,100));
        GUI.Label(new Rect(Screen.width / 2 - 375, Screen.height / 2 + 30, 560, 250), "Building Stats", myGUIStyle);
        GUI.Label(new Rect(Screen.width / 2 - 100, Screen.height / 2 + 60, labelLength, 30), "Residential");
        GUI.Label(new Rect(Screen.width / 2 - 130, Screen.height / 2 + 120, 140, 140), iconResBuild);
		GUI.Button(new Rect(Screen.width / 2 - 90, Screen.height / 2 + 80, 30, 30),"100");
        if (GUI.Button(new Rect(Screen.width / 2 - 60, Screen.height / 2 + 80, 30, 30), plus) && (fragments - UpgradeCost) >= 0)
        {
            if (citybarres <= 80)
            {
                fragments -= UpgradeCost;
                citybarres += upgradeInterval;
            }
            if (citybarres >= 80 && citybarres < textureBarLength && (fragments - UpgradeCost2) >= 0)
            {
                fragments -= UpgradeCost2;
                citybarres += upgradeInterval;
            }
        }
        GUI.Label(new Rect(Screen.width / 2 - 300, Screen.height / 2 + 60, labelLength, 30), "Defense");
        GUI.Label(new Rect(Screen.width / 2 - 330, Screen.height / 2 + 120, 140, 140), iconsattBuild2);
			GUI.Button(new Rect(Screen.width / 2 - 290, Screen.height / 2 + 80, 30, 30),"100");
        if (GUI.Button(new Rect(Screen.width / 2 - 260, Screen.height / 2 + 80, 30, 30), plus))
        {
            if (citybardef <= 80 && (fragments - UpgradeCost) >= 0)
            {
                fragments -= UpgradeCost;
                citybardef += upgradeInterval;
            }
            if (citybardef >= 80 && citybardef < textureBarLength && (fragments - UpgradeCost2) >= 0)
            {
                fragments -= UpgradeCost2;
                citybardef += upgradeInterval;
            }
        }
        GUI.Label(new Rect(Screen.width / 2 + 60, Screen.height / 2 + 60, labelLength, 30), "Attack");
        GUI.Label(new Rect(Screen.width / 2 + 40, Screen.height / 2 + 120, 140, 140), icondefBuild3);
			GUI.Button(new Rect(Screen.width / 2 + 60, Screen.height / 2 + 80, 30, 30),"100");
        if (GUI.Button(new Rect(Screen.width / 2 + 90, Screen.height / 2 + 80, 30, 30), plus))
        {
            if (citybarass <= 80 && (fragments - UpgradeCost) >= 0)
            {
                fragments -= UpgradeCost;
                citybarass += upgradeInterval;
            }
            if (citybarass >= 80 && citybarass < textureBarLength && (fragments - UpgradeCost2) >= 0)
            {
                fragments -= UpgradeCost2;
                citybarass += upgradeInterval;
            }
        }
        GUI.Box(new Rect(Screen.width / 2 + 200, Screen.height / 2 - 280, 160, 540), "Weapon", myGUIStyle);
        if (mainattack1)
            GUI.Label(new Rect(Screen.width / 2 + 253, Screen.height / 2 - 260, 90, 90), hovericon);
        if (mainattack2)
            GUI.Label(new Rect(Screen.width / 2 + 253, Screen.height / 2 - 180, 90, 90), hovericon);
        if (mainattack3)
            GUI.Label(new Rect(Screen.width / 2 + 253, Screen.height / 2 - 100, 90, 90), hovericon);
        if (altattack1)
            GUI.Label(new Rect(Screen.width / 2 + 253, Screen.height / 2 - 0, 90, 90), hovericon);
        if (altattack2)
            GUI.Label(new Rect(Screen.width / 2 + 253, Screen.height / 2 + 80, 90, 90), hovericon);
        if (altattack3)
            GUI.Label(new Rect(Screen.width / 2 + 253, Screen.height / 2 + 160, 90, 90), hovericon);
		
		GUI.Label(new Rect(Screen.width / 2 + 220, Screen.height / 2 - 260, 70, 70), "0");
        if (GUI.Button(new Rect(Screen.width / 2 + 250, Screen.height / 2 - 260, 70, 70), lasericon))
        {
            mainattack1 = true;
            mainattack2 = false;
            mainattack3 = false;
        }
		GUI.Label(new Rect(Screen.width / 2 + 220, Screen.height / 2 - 180, 70, 70),"200");
        if (GUI.Button(new Rect(Screen.width / 2 + 250, Screen.height / 2 - 180, 70, 70), multilasericon))
        {
            mainattack1 = false;
            mainattack2 = true;
            mainattack3 = false;
        }
		GUI.Label(new Rect(Screen.width / 2 + 220, Screen.height / 2 - 100, 70, 70),"500");
        if (GUI.Button(new Rect(Screen.width / 2 + 250, Screen.height / 2 - 100, 70, 70), shotgunlasericon))
        {
            mainattack1 = false;
            mainattack2 = false;
            mainattack3 = true;
        }
		
		GUI.Label(new Rect(Screen.width / 2 + 220, Screen.height / 2 - 0, 70, 70),"0");
        if (GUI.Button(new Rect(Screen.width / 2 + 250, Screen.height / 2 - 0, 70, 70), rocketicon))
        {
            altattack1 = true;
            altattack2 = false;
            altattack3 = false;
        }
			GUI.Label(new Rect(Screen.width / 2 + 220, Screen.height / 2 + 80, 70, 70),"400");
        if (GUI.Button(new Rect(Screen.width / 2 + 250, Screen.height / 2 + 80, 70, 70), orbicon))
        {
            altattack1 = false;
            altattack2 = true;
            altattack3 = false;
        }
			GUI.Label(new Rect(Screen.width / 2 + 220, Screen.height / 2 + 160, 70, 70),"1000");
        if (GUI.Button(new Rect(Screen.width / 2 + 250, Screen.height / 2 + 160, 70, 70), voidicon))
        {
            altattack1 = false;
            altattack2 = false;
            altattack3 = true;
        }
        userinput = "Player";
        userinput = GUI.TextField(new Rect(Screen.width / 2 - 20, Screen.height / 2 - 220, 160, 40), userinput);

        GUI.Box(new Rect(Screen.width / 2 + 20, Screen.height / 2 - 160, 60, 40), fragments.ToString());

        if (GUI.Button(new Rect(Screen.width / 2 + 80, Screen.height / 2 - 95, 90, 90), "Continue"))
        {
            PlayerPrefs.SetInt("ShieldBoost", citybardef);
            PlayerPrefs.SetInt("ResidentialBoost", citybarres);
            PlayerPrefs.SetInt("AttackBoost", citybarass / 10);

            if (citybarass > 0)
            {
                OffensiveBuilding.bulletAttack = true;
            }
            PlayerPrefs.SetInt("Currency", fragments);

            if (mainattack1)
                Controller.mainAttack = Controller.mainAttacks.Laser;
            if (mainattack2)
                Controller.mainAttack = Controller.mainAttacks.Multilaser;
            if (mainattack3)
                Controller.mainAttack = Controller.mainAttacks.Shottylaser;
            if (altattack1)
                Controller.secondaryAttack = Controller.secondaryAttacks.Rocket;
            if (altattack2)
                Controller.secondaryAttack = Controller.secondaryAttacks.Clusterbomb;
            if (altattack3)
                Controller.secondaryAttack = Controller.secondaryAttacks.Vortex;
            int currHealthBoost = healthBarLength - PlayerPrefs.GetInt("HealthBoost");
            Debug.LogWarning(currHealthBoost);
            PlayerPrefs.SetInt("HealthBoost", healthBarLength);
            PlayerPrefs.SetInt("DamageBoost", damageBarLength);
            PlayerPrefs.SetInt("FireRate", fireRateBarLength);
            Debug.Log("fireRate boost " + fireRateBarLength);
            PlayerPrefs.SetInt("Armor", armorBarLength / 10);
            PlayerPrefs.SetInt("Evasion", evasionBarLength / 2);
            //Application.LoadLevel(1);
            //upgradeEnabled = false;
            Manager.WaveResume(currHealthBoost);
            StartCoroutine(PauseResume(.5f));
            //Controller.shootSecondary = true;
        }


        //statbar

        GUI.depth = 1;

        GUI.Label(new Rect(Screen.width / 2 + 200, Screen.height / 2 - 200, 60, 540), textattack);
        GUI.Label(new Rect(Screen.width / 2 + 205, Screen.height / 2 + 40, 60, 540), textattack2);

        //health
        GUI.DrawTexture(new Rect(Screen.width / 2 - 300, Screen.height / 2 - 100, textureBarLength, 20), background);
        GUI.DrawTexture(new Rect(Screen.width / 2 - 300, Screen.height / 2 - 100, healthBarLength, 20), foreground, ScaleMode.StretchToFill);
        
        //damage
        GUI.DrawTexture(new Rect(Screen.width / 2 - 300, Screen.height / 2 - 60, textureBarLength, 20), background);
        GUI.DrawTexture(new Rect(Screen.width / 2 - 300, Screen.height / 2 - 60, damageBarLength, 20), foreground, ScaleMode.StretchToFill);

        //armor

        GUI.DrawTexture(new Rect(Screen.width / 2 - 300, Screen.height / 2 - 20, textureBarLength, 20), background);
        GUI.DrawTexture(new Rect(Screen.width / 2 - 300, Screen.height / 2 - 20, armorBarLength, 20), foreground, ScaleMode.StretchToFill);

        //support
        GUI.DrawTexture(new Rect(Screen.width / 2 - 90, Screen.height / 2 - 100, textureBarLength, 20), background);
        GUI.DrawTexture(new Rect(Screen.width / 2 - 90, Screen.height / 2 - 100, supportBarLength, 20), foreground, ScaleMode.StretchToFill);
        
        //evasion
        GUI.DrawTexture(new Rect(Screen.width / 2 - 90, Screen.height / 2 - 60, textureBarLength, 20), background);
        GUI.DrawTexture(new Rect(Screen.width / 2 - 90, Screen.height / 2 - 60, evasionBarLength, 20), foreground, ScaleMode.StretchToFill);

        //fireRate
        GUI.DrawTexture(new Rect(Screen.width / 2 - 90, Screen.height / 2 - 20, textureBarLength, 20), background);
        GUI.DrawTexture(new Rect(Screen.width / 2 - 90, Screen.height / 2 - 20, fireRateBarLength, 20), foreground, ScaleMode.StretchToFill);

        //residential
        // GUI.DrawTexture(new Rect(Screen.width/2 - 20, Screen.height/2 - 20,textureBarLength, 20), background);
        GUI.DrawTexture(new Rect(Screen.width / 2 - 120, Screen.height / 2 + 250, 90, -citybarres), resbuild, ScaleMode.StretchToFill);


        //defense
        // GUI.DrawTexture(new Rect(Screen.width/2 - 20, Screen.height/2 - 20,textureBarLength, 20), background);
        GUI.DrawTexture(new Rect(Screen.width / 2 - 300, Screen.height / 2 + 250, 90, -citybardef), defbuild, ScaleMode.StretchToFill);


        //assault
        // GUI.DrawTexture(new Rect(Screen.width/2 - 20, Screen.height/2 - 20,textureBarLength, 20), background);
        GUI.DrawTexture(new Rect(Screen.width / 2 + 60, Screen.height / 2 + 250, 90, -citybarass), attbuild, ScaleMode.StretchToFill);
    }
}